package com.wandian.audio.tools;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.util.Log;
import android.widget.RemoteViews;

import androidx.core.app.NotificationCompat;

import com.mylib.libcore.utils.ImageUtil;
import com.mylib.libcore.utils.ToastUtil;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.audio.R;
import com.wandian.audio.bean.MusicBean;
import com.wandian.audio.config.AudioConfig;
import com.wandian.audio.mvvm.audiomain.AudioMainActivity;
import com.wandian.audio.service.AudioService;

/**
 * @author ztk
 */
public class AudioNotificationUtil {
    private NotificationManager notificationManager;
    private Context mContext;

    private Notification notification;
    public static final int NOTIFICATION_ID = 10003;
    private MusicBean musicBean;
    private RemoteViews remoteViews;
    private boolean isPlaying = true;


    public AudioNotificationUtil(Context mContext, MusicBean musicBean) {
        this.musicBean = musicBean;
        this.mContext = mContext;
        notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

    }
    public void  AudioNotify(Context mContext, MusicBean musicBean,boolean isPlaying) {
        this.musicBean = musicBean;
        this.mContext = mContext;
        this.isPlaying = isPlaying;
       if(UIUtils.isEmpty(notificationManager)){
           notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
       }
       if(remoteViews!= null){
           notifyRemoteViews();
       }else {
           showNotification();
       }
    }

    /**
     * 展示通知栏
     */
    public void showNotification(){
        String id = mContext.getPackageName()+"";
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel mChannel = new NotificationChannel(id, mContext.getString(R.string.app_name), NotificationManager.IMPORTANCE_LOW);
            mChannel.setDescription("通知栏");
            mChannel.enableLights(false);
            mChannel.setLightColor(Color.BLUE);
            mChannel.enableVibration(true);
            mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
            notificationManager.createNotificationChannel(mChannel);
            notification = new NotificationCompat.Builder(mContext, id)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    //.setLargeIcon(ImageUtil.drawableToBitmap(mContext.getResources().getDrawable(R.drawable.app_logo)))
                    .setWhen(System.currentTimeMillis())
                    .setContentIntent(getPendingIntent())
                    .setCustomBigContentView(getContentView(true))
                    .setCustomContentView(getContentView(true))
                    .setPriority(NotificationCompat.PRIORITY_MAX)
                    .setTicker("正在播放")
                    .setOngoing(true)
                    .setChannelId(mChannel.getId())
                    .build();
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            notification = new NotificationCompat.Builder(mContext, id)
                    .setWhen(System.currentTimeMillis())
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentIntent(getPendingIntent())
                    .setCustomBigContentView(getContentView(true))
                    .setCustomContentView(getContentView(true))
                    .setPriority(NotificationCompat.PRIORITY_MAX)
                    .setTicker("正在播放")
                    .setOngoing(true)
                    .build();
        } else {
            notification = new NotificationCompat.Builder(mContext, id)
                    .setWhen(System.currentTimeMillis())
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentIntent(getPendingIntent())
                    .setContent(getContentView(false))
                    .setPriority(NotificationCompat.PRIORITY_MAX)
                    .setTicker("正在播放")
                    .setOngoing(true)
                    .build();
        }

        notificationManager.notify(NOTIFICATION_ID, notification);
    }


    /**
     * 通知主界面
     */
    private PendingIntent getPendingIntent() {
        Log.d("getPendingIntent","主界面");
        Intent intent = new Intent(mContext, AudioMainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("key", AudioConfig.INTOMAIN);
           //PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(context, AudioMainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
        PendingIntent pIntent = PendingIntent.getActivity(mContext, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        return  pIntent;
    }
    /**
     * 通知上一节
     */
    private PendingIntent getUpPendingIntent() {
        Intent intent = new Intent(mContext, AudioService.class);
        intent.putExtra("key", AudioConfig.AUDIOUP);
        PendingIntent pIntent = PendingIntent.getService(mContext, 2, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        return  pIntent;
    }
    /**
     * 通知下一节
     */
    private PendingIntent getNextPendingIntent() {
        Intent intent = new Intent(mContext, AudioService.class);
        intent.putExtra("key",AudioConfig.AUDIONEXT);
        PendingIntent pIntent = PendingIntent.getService(mContext, 3, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        return  pIntent;
    }
    /**
     * 通知暂停播放
     */
    private PendingIntent getControlPendingIntent() {
        ToastUtil.show("播放暂停");
        Intent intent = new Intent(mContext, AudioService.class);
        intent.putExtra("key",AudioConfig.AUDIOCONTORL);
        PendingIntent pIntent = PendingIntent.getService(mContext, 4, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        return  pIntent;
    }

    /**
     * 获取自定义通知栏view
     * @param showBigView
     * @return
     */
    private RemoteViews getContentView(boolean showBigView) {
        String musicName ="";
        String musicAuthor = "";
        String musicPic ="";
        if(!UIUtils.isEmpty(musicBean)){
            musicName = musicBean.getName();
            musicAuthor = musicBean.getNickname();
            musicPic = musicBean.getThumb();
        }
        RemoteViews remoteViews = new RemoteViews(mContext.getPackageName(), showBigView ? R.layout.audio_view_notify_big : R.layout.audio_view_notify_small);
        //修改类容
        remoteViews.setTextViewText(R.id.tv_audio_name,musicName+"");
        if(UIUtils.isEmpty(musicAuthor)){
            remoteViews.setTextViewText(R.id.tv_audio_author,"未知");
        }else {
            remoteViews.setTextViewText(R.id.tv_audio_author,musicAuthor+"");
        }
        if(!UIUtils.isEmpty(musicPic)){
            remoteViews.setImageViewBitmap(R.id.iv_sound_icon, ImageUtil.getBitmap(musicPic));
        }
        if(isPlaying){
            remoteViews.setImageViewResource(R.id.iv_player_control, R.drawable.img_sound_start);
        }else {
            remoteViews.setImageViewResource(R.id.iv_player_control, R.drawable.img_sound_stop);
        }
        //设置点击事件
        remoteViews.setOnClickPendingIntent(R.id.iv_up_level,getUpPendingIntent());
        remoteViews.setOnClickPendingIntent(R.id.iv_next_level,getNextPendingIntent());
        remoteViews.setOnClickPendingIntent(R.id.iv_player_control,getControlPendingIntent());
        return remoteViews;
    }

    private  void  notifyRemoteViews(){
        String musicName ="";
        String musicAuthor = "";
        String musicPic ="";
        if(!UIUtils.isEmpty(musicBean)){
            musicName = musicBean.getName();
            musicAuthor = musicBean.getNickname();
            musicPic = musicBean.getThumb();
        }
        //修改类容
        remoteViews.setTextViewText(R.id.tv_audio_name,musicName+"");
        if(UIUtils.isEmpty(musicAuthor)){
            remoteViews.setTextViewText(R.id.tv_audio_author,"未知");
        }else {
            remoteViews.setTextViewText(R.id.tv_audio_author,musicAuthor+"");
        }
        if(!UIUtils.isEmpty(musicPic)){
            remoteViews.setImageViewBitmap(R.id.iv_sound_icon, ImageUtil.getBitmap(musicPic));
        }
        if(isPlaying){
            remoteViews.setImageViewResource(R.id.iv_player_control, R.drawable.img_sound_start);
        }else {
            remoteViews.setImageViewResource(R.id.iv_player_control, R.drawable.img_sound_stop);
        }
        //设置点击事件
        remoteViews.setOnClickPendingIntent(R.id.iv_up_level,getUpPendingIntent());
        remoteViews.setOnClickPendingIntent(R.id.iv_next_level,getNextPendingIntent());
        remoteViews.setOnClickPendingIntent(R.id.iv_player_control,getControlPendingIntent());
    }

}
